﻿using System;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using System.Xml;

namespace DevelopersToolbox {
    public partial class SourceSelect : UserControl {
        public SourceSelect() {
            InitializeComponent();
        }

        [Category("Behavior"), Description("Data Changed")]
        public event EventHandler DataChanged;

        private void _sourceFileNameBrowse_Click(object sender, EventArgs e) {
            DialogResult results = _sourceOpenFileDialog.ShowDialog(this);
            if (results == DialogResult.OK) {
                _sourceSelectFileName.Checked = true;
                _sourceFileName.Text = _sourceOpenFileDialog.FileName;
                _sourceFileName.Select();
            }
        }

        public byte[] Data {
            get {
                if (DesignMode) { return new byte[0]; }

                byte[] data;
                if (_sourceSelectFileName.Checked) {
                    try {
                        data = File.ReadAllBytes(_sourceFileName.Text);
                    } catch (Exception ex) {
                        MessageBox.Show(this, "Could not read file '" + _sourceFileName.Text + "'\n" + ex.Message);
                        return null;
                    }
                } else {
                    data = _sourceText.Data;
                }

                return data;
            }
        }

        public XmlDocument GetDataAsXml() {
            using (Stream stream = GetDataAsStream()) {
                XmlDocument doc = new XmlDocument();
                doc.Load(stream);
                return doc;
            }
        }

        public string GetDataAsString() {
            using (Stream stream = GetDataAsStream()) {
                if (stream == null) {
                    return "";
                }
                StreamReader reader = new StreamReader(stream);
                return reader.ReadToEnd();
            }
        }

        private Stream GetDataAsStream() {
            if (Data == null) {
                return null;
            }
            return new MemoryStream(Data);
        }

        private void _sourceFileName_Enter(object sender, EventArgs e) {
            _sourceSelectFileName.Checked = true;
        }


        public void AddTextToDropDown() {
            if (_sourceSelectFileName.Checked) {
                string text = _sourceFileName.Text;
                _sourceFileName.Items.Remove(text);
                _sourceFileName.Items.Insert(0, text);
                _sourceFileName.Text = text;
            }
        }

        public void SetText(string xml) {
            _sourceText.Text = xml;
        }

        [DefaultValue(true)]
        public bool TextInputAllowed {
            get { return _sourceText.TextInputAllowed; }
            set { _sourceText.TextInputAllowed = value; }
        }

        [DefaultValue(true)]
        public bool BinaryInputAllowed {
            get { return _sourceText.BinaryInputAllowed; }
            set { _sourceText.BinaryInputAllowed = value; }
        }

        private void _sourceFileName_TextChanged(object sender, EventArgs e) {
            if (DataChanged != null) {
                DataChanged.Invoke(sender, e);
            }
        }

        private void _sourceSelectFileName_CheckedChanged(object sender, EventArgs e) {
            if (DataChanged != null) {
                DataChanged.Invoke(sender, e);
            }
        }

        private void _sourceSelectText_CheckedChanged(object sender, EventArgs e) {
            if (DataChanged != null) {
                DataChanged.Invoke(sender, e);
            }
        }

        private void _sourceText_DataChanged(object sender, EventArgs e) {
            if (DataChanged != null) {
                DataChanged.Invoke(sender, e);
            }
        }

        private void _sourceText_Enter(object sender, EventArgs e) {
            _sourceSelectText.Checked = true;
        }
    }
}